package com.sparrow.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 网站关停, 维护过滤器
 */
public class StopFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    private static final String redirectPath = "/stop.html";

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String uri = ((HttpServletRequest) request).getRequestURI();
        if(uri.equals(redirectPath) || uri.startsWith("/analyse") || uri.endsWith(".js") || uri.endsWith(".css") || uri.endsWith(".png")){
            chain.doFilter(request, response);
        }else{
            ((HttpServletResponse)response).sendRedirect(redirectPath);
        }
    }

    @Override
    public void destroy() {

    }
}
